import { app, BrowserWindow, Menu, session, globalShortcut, ipcMain } from 'electron'
import { join } from 'path'
import path from 'path'
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

//const preloadPath = app.isPackaged ? "../../preload.js" : "../preload.js";
const preloadPath = './preload.mjs'
const renderProcessApi = path.resolve(__dirname, preloadPath)

// process.env.DIST = join(process.env.DIST_ELECTRON, "../dist");
// const indexHtml = join(process.env.DIST, "index.html");
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = true
const createWindow = async () => {
  // Menu.setApplicationMenu(null)
  const win = new BrowserWindow({
    // width: 1024,
    // height: 768,
    width: 1440,
    height: 807,
    // fullscreen: true, //全屏
    title: '必哥管理系统',
    // frame: false, //直接去除导航头部
    // show: true,
    icon: path.resolve(__dirname, '../public/icon.ico'),
    webPreferences: {
      webSecurity: false,
      nodeIntegration: true,
      enableRemoteModule: true,
      contextIsolation: true,
      preload: renderProcessApi,
    }
  })

  const env = app.isPackaged ? 'production' : 'development'
  const indexHtml = {
    development: 'http://localhost:3000', // 开发环境
    production: join(__dirname, '../dist/index.html') // 生产环境
  }

  if (app.isPackaged) {
    win.loadFile(indexHtml[env])
  } else {
    win.loadURL(indexHtml[env])
  }

  win.webContents.openDevTools()

  // 全屏切换
  ipcMain.handle('fullscreen-toggle',(event,isFullScreen)=>{
    win.setFullScreen(isFullScreen);
  })

}
app.whenReady().then(async () => {
  createWindow()
  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})
